//=============文件与数据================
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#include "tlinklist.h"
#include "slinklist.h"
#include "function.h"

//文件->双向链表(读出数据)

bool Read_Data_P(pLink PL)
{
	FILE* fp; 
	if((fp=fopen("data/AAA_Principal.txt","r")) == NULL)
	{
		//数据加载失败,程序结束
		printf("校长数据为空\n");
		exit(0);
	}
	while(!feof(fp)) fscanf(fp,"%s\n",PL->password);
	fclose(fp);

	printf("校长数据加载成功!\n");
	return true;
}//读取校长信息

bool Read_Data_T(tLink TL)
{
	FILE* ft;
	if((ft=fopen("data/AA_Teacher.txt","r")) == NULL)
	{
		//数据加载失败,程序结束
		printf("老师数据为空\n");
		exit(0);
	}
	//临时参数
	tNode* tmp = (tNode*)malloc(sizeof(tNode));
	while(!feof(ft))
	{
		
		fscanf(ft,"%d%s%s%s%hhd\n",&tmp->id,tmp->password,tmp->name,tmp->phone,&tmp->state);
		Insert_tLink(TL,tmp->id,tmp->password,tmp->name,tmp->phone,tmp->state);
	}
	free(tmp);

	fclose(ft);

	printf("教师数据加载成功!\n");
	return true;
}//读取老师信息

bool Read_Data_S(sLink SL)
{
	FILE* fs;
	if((fs=fopen("data/A_Student.txt","r")) == NULL)
	{
		//数据加载失败,程序结束
		printf("学生数据为空\n");
		exit(0);
	}
	//临时参数
	sNode* tmp = (sNode*)malloc(sizeof(sNode));
	while(!feof(fs))
	{
		
		fscanf(fs,"%d%s%s%s%s%s%hhd%f%f%f\n",&tmp->id,tmp->password,tmp->name,tmp->sex,tmp->idcard,tmp->phone,&tmp->state,&tmp->ChineseScore,&tmp->MathScore,&tmp->EnglishScore);
		Insert_sLink(SL,tmp->id,tmp->password,tmp->name,tmp->sex,tmp->idcard,tmp->phone,tmp->state);
		Score_sLink(SL,tmp->id,tmp->ChineseScore,tmp->MathScore,tmp->EnglishScore);
	}
	free(tmp);

	fclose(fs);

	printf("学生数据加载成功!\n");
	return true;	
}//读取学生信息

//双向链表->文件(存入数据)

bool Write_Data_P(pLink PL)
{
	FILE* fp;
	if((fp = fopen("data/AAA_Principal.txt","w")) == NULL)
	{
		printf("校长数据无法存储\n");
		return false;
	}
	fprintf(fp,"%s\n",PL->password);

	free(PL);

	fclose(fp);

	printf("校长数据存储成功!\n");
	return true;
}//存入校长信息

bool Write_Data_T(tLink TL)
{
	FILE* ft;
	if((ft = fopen("data/AA_Teacher.txt","w")) == NULL)
	{
		printf("教师数据无法存储\n");
		return false;
	}
	tNode* node = TL->head;
	tNode* tmp = node;
	while(node != NULL)
	{
		fprintf(ft,"%d %s %s %s %hhd\n",node->id,node->password,node->name,node->phone,node->state);
		tmp = node;
		node = node->next;
		
		free(tmp);
	}

	free(TL);

	fclose(ft);
	
	printf("教师数据存储成功!\n");
	return true;
}//存入老师信息

bool Write_Data_S(sLink SL)
{
	FILE* fs;
	if((fs = fopen("data/A_Student.txt","w")) == NULL)
	{
		printf("学生数据无法存储\n");
		return false;
	}
	sNode* node = SL->head;
	sNode* tmp = node;
	while(node != NULL)
	{
		fprintf(fs,"%d %s %s %s %s %s %hhd %.2lf %.2lf %.2lf\n",node->id,node->password,node->name,node->sex,node->idcard,node->phone,node->state,node->ChineseScore,node->MathScore,node->EnglishScore);
		tmp = node;
		node = node->next;

		free(tmp);
	}

	free(SL);

	fclose(fs);

	printf("学生数据存储成功!\n");
	return true;
}//存入学生信息

//添加到双向链表(批量添加)

bool Add_Data_S(sLink SL)
{
	FILE* fs;
	if((fs = fopen("Add_Student.txt","r")) == NULL)
	{
		printf("学生信息批量导入失败\n");
		return false;
	}
	//临时参数
	sNode* tmp = (sNode*)malloc(sizeof(sNode));
	while(!feof(fs))
	{
		fscanf(fs,"%d%s%s%s%s%s%hhd\n",&tmp->id,tmp->password,tmp->name,tmp->sex,tmp->idcard,tmp->phone,&tmp->state);
		Insert_sLink(SL,tmp->id,tmp->password,tmp->name,tmp->sex,tmp->idcard,tmp->phone,tmp->state);
	}
	free(tmp);

	fclose(fs);

	printf("学生信息批量导入成功!\n");
	return true;
}//添加学生信息

bool Add_Data_Score(sLink SL)
{
	
	FILE* fs;
	if((fs = fopen("Add_Score.txt","r")) == NULL)
	{
		printf("学生分数批量导入失败\n");
		return false;
	}
	//临时参数
	sNode* tmp = (sNode*)malloc(sizeof(sNode));
	while(!feof(fs))
	{
		fscanf(fs,"%d%f%f%f\n",&tmp->id,&tmp->ChineseScore,&tmp->MathScore,&tmp->EnglishScore);
		Score_sLink(SL,tmp->id,tmp->ChineseScore,tmp->MathScore,tmp->EnglishScore);
	}
	free(tmp);

	fclose(fs);

	printf("学生成绩批量导入成功!\n");
	return true;
}//添加学生分数

